package com.example.fz338.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.fz338.R;
import com.example.fz338.bean.NewsBean;
import com.example.fz338.utils.NetUtils;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
/**
 * 自定义布局，下面是常见的图片样式，更多实现可以看demo，可以自己随意发挥
 */
public class ImageTitleNumAdapter extends BannerAdapter<NewsBean, ImageTitleNumAdapter.BannerViewHolder> {
    public ImageTitleNumAdapter(List<NewsBean> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.banner_image_title_num, parent, false);
        return new BannerViewHolder(view);
    }

    // 绑定数据
    @Override
    public void onBindView(BannerViewHolder holder, NewsBean data, int position, int size) {
        Glide.with(holder.imageView).load(NetUtils.BASE_URL + data.getImg1()).into(holder.imageView);
        holder.title.setText(data.getNewsName());
        holder.numIndicator.setText(String.format(Locale.CHINA, "%d/%d", position + 1, size));
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView title;
        TextView numIndicator;

        public BannerViewHolder(@NonNull View view) {
            super(view);
            imageView = view.findViewById(R.id.image);
            title = view.findViewById(R.id.bannerTitle);
            numIndicator = view.findViewById(R.id.numIndicator);
        }
    }
}
